package utils;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import model.User;
import model.Usertype;
import dtos.UsertypeDTO;

/**
 * Session Bean implementation class UsertypeDAO
 */
@Stateless
@LocalBean
public class UsertypeDAO implements UsertypeDAORemote {

    /**
     * Default constructor. 
     */
	
	@PersistenceContext(unitName = "rmtPersistenceUnit")
	EntityManager em;
	
	@EJB
	Conv conv;
	
    public UsertypeDAO() {
    }

    /**
     * Method used to insert usertype 
     * @param usertype ud
     */
    @Override
	public void insertUsertype(UsertypeDTO ud) {
		Usertype r = em.find(Usertype.class, ud.getId());
		if(r==null){
			Usertype r1 = conv.fromEntityUsertypeDTO(ud);
			em.persist(r1);
		}
		else{
			em.merge(r);
		}
	}
    
    /**
     * Method used to update usertype
     * @param usertype
     */
    @Override
    public void updateUsertype(UsertypeDTO ud){
    	if(em.find(Usertype.class, ud.getId())!=null)
    	  em.merge(conv.fromEntityUsertypeDTO(ud));
    }
    
    /**
     * Method used to getUsertype 
     * @param id 
     * @return usertype
     */
    @Override
	public UsertypeDTO getUsertype(int id) {
		return conv.fromEntityUsertype(em.find(Usertype.class, id));
	}
    
    /**
     * Method used to get all usertypes by id
     * @param userId
     * @return usertypeDto
     */
    @Override
    public UsertypeDTO getAllUsertypesByUserId(int userId)
    {
    	User u=em.find(User.class, userId);
		return new Conv().fromEntityUsertype(u.getUsertype());
    }
    
    /**
     * Method used to delete Usertype
     * @param ud of type Usertype
     */
    @Override
    public void deleteUsertype(UsertypeDTO ud){
		Usertype r = em.find(Usertype.class, ud.getId());
        em.remove(r);
	}
    
    /**
     * Method used to get all usertypes
     * @return list of usertypes
     */
    @Override
    public List<UsertypeDTO> getAllUsertypes() {
    	List<UsertypeDTO> resultlist = new ArrayList<>();
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Usertype> cq = cb.createQuery(Usertype.class);
		Root<Usertype> dom = cq.from(Usertype.class);
		cq.select(dom);
		TypedQuery<Usertype> q = em.createQuery(cq);

		List<Usertype> usertype = q.getResultList();
		
		for (Usertype d : usertype) {
			resultlist.add(conv.fromEntityUsertype(d));
		}
		return resultlist;
    }
    
    /**
     * Method used to get usertype 
     * @param name
     * @return UsertypeDto
     */
    @Override
    public UsertypeDTO getUsertypeByName(String name) {
    	CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Usertype> cq = cb.createQuery(Usertype.class);
		Root<Usertype> dom = cq.from(Usertype.class);
		cq.select(dom);
		TypedQuery<Usertype> q = em.createQuery(cq);

		List<Usertype> usertype = q.getResultList();
		
		for (Usertype d : usertype) {
			if (d.getName().equals(name)) {
				return conv.fromEntityUsertype(d);
			}
		}
		return null;
    }
   
}
